package com.hikvision.hrms.controller;

import com.hikvision.hrms.model.Admin;
import com.hikvision.hrms.model.Post;
import com.hikvision.hrms.service.AdminService;
import com.hikvision.hrms.service.PostService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : yangliu449
 * @date :  2019/2/18
 */
//公告管理
@Controller
public class PostController {
    @Autowired
    PostService postService;
   @Autowired
    AdminService adminService;
    int  total ;
    @RequestMapping("/postData")
    @ResponseBody
    public List<Post>  postData(){
        List<Post>  list = postService.postData();
        total = list.size()+1;
        return  list;
    }


    //   显示管理员页面
    @RequestMapping("/showPost")
    @ResponseBody
    public ModelAndView showPost(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/post");
        return modelAndView;
    }
    @RequestMapping("/addPost")
    @ResponseBody
    public void addPost(Integer id, String title, String content, String  date,String createname){
        id = total;
        List<Admin> list = adminService.adminData();
        Post post = new Post();
        post.setId(id);
        post.setDate(date);
        post.setContent(content);
        post.setTitle(title);
        for (Admin admin : list){
            if(admin.getUsername().equals(createname)){
                post.setAdmin(admin);
                postService.addPost(post);
                break;
            }
        }
    }
    @RequestMapping("/updatePost")
    @ResponseBody
    public  void updatePost(Integer id, String title, String content, String  date,String createname){
        int admin_id =  postService.getIdByUsername(createname);
        System.out.println(title);
        Admin admin = new Admin();
        admin.setId(admin_id);
        admin.setUsername(createname);
        Post post = new Post();
        post.setId(id);
        post.setAdmin(admin);
        post.setDate(date);
        post.setTitle(title);
        post.setContent(content);
        postService.updatePost(post);
    }

    @RequestMapping("/deletePost")
    @ResponseBody
    public void  deletePost(Integer id, String title, String content, String  date,String createname){
        postService.deletePost(id);
    }
}
